package com.bilibili.bilibili.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.VolleyError;
import com.bilibili.bilibili.R;
import com.bilibili.bilibili.adapter.MyRecyclerAdapter;
import com.bilibili.bilibili.model.LiveModel;
import com.bilibili.bilibili.network.HttpGetData;
import com.bilibili.bilibili.network.VolleyInterface;
import com.bilibili.bilibili.utils.GlideImageLoader;
import com.bilibili.bilibili.utils.GsonUtil;
import com.bilibili.bilibili.utils.LogUtil;
import com.bilibili.bilibili.utils.URLUtils;
import com.google.gson.reflect.TypeToken;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class LiveFragment extends Fragment {


    private View mView;
    private Banner live_banner;
    private List<String> mStrings;
    private RecyclerView live_recycler;
    private SwipeRefreshLayout swiperefreshlayout;

    public LiveFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mView = inflater.inflate(R.layout.fragment_live, container, false);
        initView(mView);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        HttpGetData.RequestGet(getActivity(), URLUtils.LIVE_URL, "gcp1", new VolleyInterface(getActivity(), VolleyInterface.mListener, VolleyInterface.mErrorListener) {
            @Override
            public void onMySuccess(String result) {
                mStrings = new ArrayList<String>();
                LiveModel liveModel = GsonUtil.parseJsonToObj(result, new TypeToken<LiveModel>() {
                });
                List<LiveModel.DataBean.BannerBean> banner = liveModel.getData().getBanner();
                LogUtil.i("TAG", banner.toString());
                for (int i = 0; i < banner.size(); i++) {
                    String img = banner.get(i).getImg();
                    mStrings.add(img);
                }
                live_banner.setImageLoader(new GlideImageLoader());
                live_banner.setImages(mStrings);
                live_banner.start();
                List<LiveModel.DataBean.PartitionsBean> partitions = liveModel.getData().getPartitions();
                MyRecyclerAdapter myRecyclerAdapter = new MyRecyclerAdapter(partitions, getActivity());
                live_recycler.setAdapter(myRecyclerAdapter);
            }

            @Override
            public void onMyError(VolleyError error) {

            }
        });
    }

    private void initView(View mView) {
        live_banner = (Banner) mView.findViewById(R.id.live_banner);
        live_recycler = (RecyclerView) mView.findViewById(R.id.live_recycler);
        swiperefreshlayout = (SwipeRefreshLayout) mView.findViewById(R.id.swiperefreshlayout);
        swiperefreshlayout.setColorSchemeColors(Color.BLUE, Color.GREEN);
        swiperefreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                swiperefreshlayout.setRefreshing(false);
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setSmoothScrollbarEnabled(true);
        linearLayoutManager.setAutoMeasureEnabled(true);
        live_recycler.setLayoutManager(linearLayoutManager);
        live_recycler.setHasFixedSize(true);
        live_recycler.setNestedScrollingEnabled(false);//88,89,91,92行解决了NestedScrollView嵌套RecyclerView滑动不顺畅的问题
    }
}
